package com.neo.common.utils;

public class StringUtils extends org.apache.commons.lang3.StringUtils {


    public static boolean isNullOrEmpty(Object obj) {
        return (obj == null) || ("".equals(obj.toString()));
    }

    public static String toUpCaseFirst(String str) {
        if ((str == null) || ("".equals(str))) {
            return str;
        }
        char[] temp = str.toCharArray();
        temp[0] = str.toUpperCase().toCharArray()[0];
        str = String.valueOf(temp);

        return str;
    }

    public static String toLowCaseFirst(String str) {
        if ((str == null) || ("".equals(str))) {
            return str;
        }
        char[] temp = str.toCharArray();
        temp[0] = str.toLowerCase().toCharArray()[0];
        str = String.valueOf(temp);

        return str;
    }

    public static String[] toUpCaseFirst(String[] str) {
        if ((str == null) || (str.length == 0)) {
            return str;
        }
        String[] result = new String[str.length];
        for (int i = 0; i < result.length; i++) {
            result[i] = toUpCaseFirst(str[i]);
        }

        return result;
    }

    public static String[] toLowCaseFirst(String[] str) {
        if ((str == null) || (str.length == 0)) {
            return str;
        }
        String[] result = new String[str.length];
        for (int i = 0; i < result.length; i++) {
            result[i] = toLowCaseFirst(str[i]);
        }

        return result;
    }

    public static String makeAllWordFirstLetterUpperCase(String sqlName) {
        String[] strs = sqlName.toLowerCase().split("_");
        String result = "";
        String preStr = "";
        for (int i = 0; i < strs.length; i++) {
            if (preStr.length() == 1)
                result = result + strs[i];
            else {
                result = result + capitalize(strs[i]);
            }
            preStr = strs[i];
        }
        return result;
    }

    public static String replace(String inString, String oldPattern, String newPattern) {
        if (inString == null) {
            return null;
        }
        if ((oldPattern == null) || (newPattern == null)) {
            return inString;
        }

        StringBuffer sbuf = new StringBuffer();
        int pos = 0;
        int index = inString.indexOf(oldPattern);
        int patLen = oldPattern.length();
        while (index >= 0) {
            sbuf.append(inString.substring(pos, index));
            sbuf.append(newPattern);
            pos = index + patLen;
            index = inString.indexOf(oldPattern, pos);
        }
        sbuf.append(inString.substring(pos));

        return sbuf.toString();
    }

    public static String capitalize(String str) {
        return changeFirstCharacterCase(str, true);
    }

    public static String uncapitalize(String str) {
        return changeFirstCharacterCase(str, false);
    }

    private static String changeFirstCharacterCase(String str, boolean capitalize) {
        if ((str == null) || (str.length() == 0)) {
            return str;
        }
        StringBuffer buf = new StringBuffer(str.length());
        if (capitalize)
            buf.append(Character.toUpperCase(str.charAt(0)));
        else {
            buf.append(Character.toLowerCase(str.charAt(0)));
        }
        buf.append(str.substring(1));
        return buf.toString();
    }

    public static String concat(String... args) {
        StringBuffer sbuf = new StringBuffer("");
        for (String arg : args) {
            if (org.apache.commons.lang.StringUtils.isNotEmpty(arg)) {
                sbuf.append(arg);
            }
        }
        return sbuf.toString();
    }
}
